package com.tengju.support.infrastructure.repository.article.convert;

import com.tengju.support.domain.model.article.Article;
import com.tengju.support.domain.model.article.ArticleId;
import com.tengju.support.domain.model.article.ArticleType;
import com.tengju.support.domain.shared.IdObject;
import com.tengju.support.infrastructure.persistence.mybatis.dataobject.ArticleDO;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Optional;

public class ArticleConvert {

    public static Article convertToModel(ArticleDO articleDO) {
        if (articleDO == null) {
            return null;
        }
        return new Article(Optional.ofNullable(articleDO.getArticleId()).map(ArticleId::new).orElse(null),
                articleDO.getTitle(),
                ArticleType.fromVal(articleDO.getType()),
                articleDO.getContext(),
                articleDO.getCreateBy(),
                articleDO.getUpdateBy(),
                articleDO.getCreateTime(),
                articleDO.getViewCnt());
    }

    public static ArticleDO convertToDO(Article article) {
        if (article == null) {
            return null;
        }
        return new ArticleDO(Optional.ofNullable(article.getArticleId()).map(IdObject::getValue).orElse(null),
                article.getTitle(),
                article.getArticleType().getVal(),
                article.getContext(),
                article.getCreateBy(),
                article.getUpdateBy(),
                null ,null,
                article.getViewCnt());
    }
}
